gpu.cpp 15 KB

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