gpu.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/common_types.h"
  5. #include "core/arm/arm_interface.h"
  6. #include "core/settings.h"
  7. #include "core/core.h"
  8. #include "core/mem_map.h"
  9. #include "core/core_timing.h"
  10. #include "core/hle/hle.h"
  11. #include "core/hle/service/gsp_gpu.h"
  12. #include "core/hle/service/dsp_dsp.h"
  13. #include "core/hw/gpu.h"
  14. #include "video_core/command_processor.h"
  15. #include "video_core/utils.h"
  16. #include "video_core/video_core.h"
  17. #include <video_core/color.h>
  18. namespace GPU {
  19. Regs g_regs;
  20. /// True if the current frame was skipped
  21. bool g_skip_frame = false;
  22. /// 268MHz / gpu_refresh_rate frames per second
  23. static u64 frame_ticks;
  24. /// Event id for CoreTiming
  25. static int vblank_event;
  26. /// Total number of frames drawn
  27. static u64 frame_count;
  28. /// True if the last frame was skipped
  29. static bool last_skip_frame = false;
  30. template <typename T>
  31. inline void Read(T &var, const u32 raw_addr) {
  32. u32 addr = raw_addr - 0x1EF00000;
  33. u32 index = addr / 4;
  34. // Reads other than u32 are untested, so I'd rather have them abort than silently fail
  35. if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) {
  36. LOG_ERROR(HW_GPU, "unknown Read%lu @ 0x%08X", sizeof(var) * 8, addr);
  37. return;
  38. }
  39. var = g_regs[addr / 4];
  40. }
  41. template <typename T>
  42. inline void Write(u32 addr, const T data) {
  43. addr -= 0x1EF00000;
  44. u32 index = addr / 4;
  45. // Writes other than u32 are untested, so I'd rather have them abort than silently fail
  46. if (index >= Regs::NumIds() || !std::is_same<T, u32>::value) {
  47. LOG_ERROR(HW_GPU, "unknown Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32)data, addr);
  48. return;
  49. }
  50. g_regs[index] = static_cast<u32>(data);
  51. switch (index) {
  52. // Memory fills are triggered once the fill value is written.
  53. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[0].trigger, 0x00004 + 0x3):
  54. case GPU_REG_INDEX_WORKAROUND(memory_fill_config[1].trigger, 0x00008 + 0x3):
  55. {
  56. const bool is_second_filler = (index != GPU_REG_INDEX(memory_fill_config[0].trigger));
  57. auto& config = g_regs.memory_fill_config[is_second_filler];
  58. if (config.address_start && config.trigger) {
  59. u8* start = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetStartAddress()));
  60. u8* end = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetEndAddress()));
  61. if (config.fill_24bit) {
  62. // fill with 24-bit values
  63. for (u8* ptr = start; ptr < end; ptr += 3) {
  64. ptr[0] = config.value_24bit_r;
  65. ptr[1] = config.value_24bit_g;
  66. ptr[2] = config.value_24bit_b;
  67. }
  68. } else if (config.fill_32bit) {
  69. // fill with 32-bit values
  70. for (u32* ptr = (u32*)start; ptr < (u32*)end; ++ptr)
  71. *ptr = config.value_32bit;
  72. } else {
  73. // fill with 16-bit values
  74. for (u16* ptr = (u16*)start; ptr < (u16*)end; ++ptr)
  75. *ptr = config.value_16bit;
  76. }
  77. LOG_TRACE(HW_GPU, "MemoryFill from 0x%08x to 0x%08x", config.GetStartAddress(), config.GetEndAddress());
  78. config.trigger = 0;
  79. config.finished = 1;
  80. if (!is_second_filler) {
  81. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC0);
  82. } else {
  83. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PSC1);
  84. }
  85. }
  86. break;
  87. }
  88. case GPU_REG_INDEX(display_transfer_config.trigger):
  89. {
  90. const auto& config = g_regs.display_transfer_config;
  91. if (config.trigger & 1) {
  92. u8* src_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalInputAddress()));
  93. u8* dst_pointer = Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalOutputAddress()));
  94. if (config.scaling > config.ScaleXY) {
  95. LOG_CRITICAL(HW_GPU, "Unimplemented display transfer scaling mode %u", config.scaling.Value());
  96. UNIMPLEMENTED();
  97. break;
  98. }
  99. unsigned horizontal_scale = (config.scaling != config.NoScale) ? 2 : 1;
  100. unsigned vertical_scale = (config.scaling == config.ScaleXY) ? 2 : 1;
  101. u32 output_width = config.output_width / horizontal_scale;
  102. u32 output_height = config.output_height / vertical_scale;
  103. if (config.raw_copy) {
  104. // Raw copies do not perform color conversion nor tiled->linear / linear->tiled conversions
  105. // TODO(Subv): Verify if raw copies perform scaling
  106. memcpy(dst_pointer, src_pointer, config.output_width * config.output_height *
  107. GPU::Regs::BytesPerPixel(config.output_format));
  108. LOG_TRACE(HW_GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%ux%u)-> 0x%08x(%ux%u), flags 0x%08X, Raw copy",
  109. config.output_height * output_width * GPU::Regs::BytesPerPixel(config.output_format),
  110. config.GetPhysicalInputAddress(), config.input_width.Value(), config.input_height.Value(),
  111. config.GetPhysicalOutputAddress(), config.output_width.Value(), config.output_height.Value(),
  112. config.output_format.Value(), config.flags);
  113. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF);
  114. break;
  115. }
  116. // TODO(Subv): Implement the box filter when scaling is enabled
  117. // right now we're just skipping the extra pixels.
  118. for (u32 y = 0; y < output_height; ++y) {
  119. for (u32 x = 0; x < output_width; ++x) {
  120. Math::Vec4<u8> src_color = { 0, 0, 0, 0 };
  121. u32 scaled_x = x * horizontal_scale;
  122. u32 scaled_y = y * vertical_scale;
  123. u32 dst_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.output_format);
  124. u32 src_bytes_per_pixel = GPU::Regs::BytesPerPixel(config.input_format);
  125. u32 src_offset;
  126. u32 dst_offset;
  127. if (config.output_tiled) {
  128. // Interpret the input as linear and the output as tiled
  129. u32 coarse_y = y & ~7;
  130. u32 stride = output_width * dst_bytes_per_pixel;
  131. src_offset = (scaled_x + scaled_y * config.input_width) * src_bytes_per_pixel;
  132. dst_offset = VideoCore::GetMortonOffset(x, y, dst_bytes_per_pixel) + coarse_y * stride;
  133. } else {
  134. // Interpret the input as tiled and the output as linear
  135. u32 coarse_y = scaled_y & ~7;
  136. u32 stride = config.input_width * src_bytes_per_pixel;
  137. src_offset = VideoCore::GetMortonOffset(scaled_x, scaled_y, src_bytes_per_pixel) + coarse_y * stride;
  138. dst_offset = (x + y * output_width) * dst_bytes_per_pixel;
  139. }
  140. const u8* src_pixel = src_pointer + src_offset;
  141. switch (config.input_format) {
  142. case Regs::PixelFormat::RGBA8:
  143. src_color = Color::DecodeRGBA8(src_pixel);
  144. break;
  145. case Regs::PixelFormat::RGB8:
  146. src_color = Color::DecodeRGB8(src_pixel);
  147. break;
  148. case Regs::PixelFormat::RGB565:
  149. src_color = Color::DecodeRGB565(src_pixel);
  150. break;
  151. case Regs::PixelFormat::RGB5A1:
  152. src_color = Color::DecodeRGB5A1(src_pixel);
  153. break;
  154. case Regs::PixelFormat::RGBA4:
  155. src_color = Color::DecodeRGBA4(src_pixel);
  156. break;
  157. default:
  158. LOG_ERROR(HW_GPU, "Unknown source framebuffer format %x", config.input_format.Value());
  159. break;
  160. }
  161. u8* dst_pixel = dst_pointer + dst_offset;
  162. switch (config.output_format) {
  163. case Regs::PixelFormat::RGBA8:
  164. Color::EncodeRGBA8(src_color, dst_pixel);
  165. break;
  166. case Regs::PixelFormat::RGB8:
  167. Color::EncodeRGB8(src_color, dst_pixel);
  168. break;
  169. case Regs::PixelFormat::RGB565:
  170. Color::EncodeRGB565(src_color, dst_pixel);
  171. break;
  172. case Regs::PixelFormat::RGB5A1:
  173. Color::EncodeRGB5A1(src_color, dst_pixel);
  174. break;
  175. case Regs::PixelFormat::RGBA4:
  176. Color::EncodeRGBA4(src_color, dst_pixel);
  177. break;
  178. default:
  179. LOG_ERROR(HW_GPU, "Unknown destination framebuffer format %x", config.output_format.Value());
  180. break;
  181. }
  182. }
  183. }
  184. LOG_TRACE(HW_GPU, "DisplayTriggerTransfer: 0x%08x bytes from 0x%08x(%ux%u)-> 0x%08x(%ux%u), dst format %x, flags 0x%08X",
  185. config.output_height * output_width * GPU::Regs::BytesPerPixel(config.output_format),
  186. config.GetPhysicalInputAddress(), config.input_width.Value(), config.input_height.Value(),
  187. config.GetPhysicalOutputAddress(), output_width, output_height,
  188. config.output_format.Value(), config.flags);
  189. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PPF);
  190. }
  191. break;
  192. }
  193. // Seems like writing to this register triggers processing
  194. case GPU_REG_INDEX(command_processor_config.trigger):
  195. {
  196. const auto& config = g_regs.command_processor_config;
  197. if (config.trigger & 1)
  198. {
  199. u32* buffer = (u32*)Memory::GetPointer(Memory::PhysicalToVirtualAddress(config.GetPhysicalAddress()));
  200. Pica::CommandProcessor::ProcessCommandList(buffer, config.size);
  201. }
  202. break;
  203. }
  204. default:
  205. break;
  206. }
  207. }
  208. // Explicitly instantiate template functions because we aren't defining this in the header:
  209. template void Read<u64>(u64 &var, const u32 addr);
  210. template void Read<u32>(u32 &var, const u32 addr);
  211. template void Read<u16>(u16 &var, const u32 addr);
  212. template void Read<u8>(u8 &var, const u32 addr);
  213. template void Write<u64>(u32 addr, const u64 data);
  214. template void Write<u32>(u32 addr, const u32 data);
  215. template void Write<u16>(u32 addr, const u16 data);
  216. template void Write<u8>(u32 addr, const u8 data);
  217. /// Update hardware
  218. static void VBlankCallback(u64 userdata, int cycles_late) {
  219. frame_count++;
  220. last_skip_frame = g_skip_frame;
  221. g_skip_frame = (frame_count & Settings::values.frame_skip) != 0;
  222. // Swap buffers based on the frameskip mode, which is a little bit tricky. When
  223. // a frame is being skipped, nothing is being rendered to the internal framebuffer(s).
  224. // So, we should only swap frames if the last frame was rendered. The rules are:
  225. // - If frameskip == 0 (disabled), always swap buffers
  226. // - If frameskip == 1, swap buffers every other frame (starting from the first frame)
  227. // - If frameskip > 1, swap buffers every frameskip^n frames (starting from the second frame)
  228. if ((((Settings::values.frame_skip != 1) ^ last_skip_frame) && last_skip_frame != g_skip_frame) ||
  229. Settings::values.frame_skip == 0) {
  230. VideoCore::g_renderer->SwapBuffers();
  231. }
  232. // Signal to GSP that GPU interrupt has occurred
  233. // TODO(yuriks): hwtest to determine if PDC0 is for the Top screen and PDC1 for the Sub
  234. // screen, or if both use the same interrupts and these two instead determine the
  235. // beginning and end of the VBlank period. If needed, split the interrupt firing into
  236. // two different intervals.
  237. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC0);
  238. GSP_GPU::SignalInterrupt(GSP_GPU::InterruptId::PDC1);
  239. // TODO(bunnei): Fake a DSP interrupt on each frame. This does not belong here, but
  240. // until we can emulate DSP interrupts, this is probably the only reasonable place to do
  241. // this. Certain games expect this to be periodically signaled.
  242. DSP_DSP::SignalInterrupt();
  243. // Reschedule recurrent event
  244. CoreTiming::ScheduleEvent(frame_ticks - cycles_late, vblank_event);
  245. }
  246. /// Initialize hardware
  247. void Init() {
  248. auto& framebuffer_top = g_regs.framebuffer_config[0];
  249. auto& framebuffer_sub = g_regs.framebuffer_config[1];
  250. // Setup default framebuffer addresses (located in VRAM)
  251. // .. or at least these are the ones used by system applets.
  252. // There's probably a smarter way to come up with addresses
  253. // like this which does not require hardcoding.
  254. framebuffer_top.address_left1 = 0x181E6000;
  255. framebuffer_top.address_left2 = 0x1822C800;
  256. framebuffer_top.address_right1 = 0x18273000;
  257. framebuffer_top.address_right2 = 0x182B9800;
  258. framebuffer_sub.address_left1 = 0x1848F000;
  259. framebuffer_sub.address_left2 = 0x184C7800;
  260. //framebuffer_sub.address_right1 = unknown;
  261. //framebuffer_sub.address_right2 = unknown;
  262. framebuffer_top.width = 240;
  263. framebuffer_top.height = 400;
  264. framebuffer_top.stride = 3 * 240;
  265. framebuffer_top.color_format = Regs::PixelFormat::RGB8;
  266. framebuffer_top.active_fb = 0;
  267. framebuffer_sub.width = 240;
  268. framebuffer_sub.height = 320;
  269. framebuffer_sub.stride = 3 * 240;
  270. framebuffer_sub.color_format = Regs::PixelFormat::RGB8;
  271. framebuffer_sub.active_fb = 0;
  272. frame_ticks = 268123480 / Settings::values.gpu_refresh_rate;
  273. last_skip_frame = false;
  274. g_skip_frame = false;
  275. vblank_event = CoreTiming::RegisterEvent("GPU::VBlankCallback", VBlankCallback);
  276. CoreTiming::ScheduleEvent(frame_ticks, vblank_event);
  277. LOG_DEBUG(HW_GPU, "initialized OK");
  278. }
  279. /// Shutdown hardware
  280. void Shutdown() {
  281. LOG_DEBUG(HW_GPU, "shutdown OK");
  282. }
  283. } // namespace