renderer_opengl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "core/hw/gpu.h"
  5. #include "video_core/video_core.h"
  6. #include "video_core/renderer_opengl/renderer_opengl.h"
  7. #include "video_core/renderer_opengl/gl_shader_util.h"
  8. #include "video_core/renderer_opengl/gl_shaders.h"
  9. #include "core/mem_map.h"
  10. #include <algorithm>
  11. static const GLfloat kViewportAspectRatio =
  12. (static_cast<float>(VideoCore::kScreenTopHeight) + VideoCore::kScreenBottomHeight) / VideoCore::kScreenTopWidth;
  13. // Fullscreen quad dimensions
  14. static const GLfloat kTopScreenWidthNormalized = 2;
  15. static const GLfloat kTopScreenHeightNormalized = kTopScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenTopHeight) / VideoCore::kScreenTopWidth);
  16. static const GLfloat kBottomScreenWidthNormalized = kTopScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenBottomWidth) / VideoCore::kScreenTopWidth);
  17. static const GLfloat kBottomScreenHeightNormalized = kBottomScreenWidthNormalized * (static_cast<float>(VideoCore::kScreenBottomHeight) / VideoCore::kScreenBottomWidth);
  18. static const GLfloat g_vbuffer_top[] = {
  19. // x, y z u v
  20. -1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
  21. 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
  22. 1.0f, kTopScreenHeightNormalized, 0.0f, 1.0f, 0.0f,
  23. 1.0f, kTopScreenHeightNormalized, 0.0f, 1.0f, 0.0f,
  24. -1.0f, kTopScreenHeightNormalized, 0.0f, 0.0f, 0.0f,
  25. -1.0f, 0.0f, 0.0f, 0.0f, 1.0f
  26. };
  27. static const GLfloat g_vbuffer_bottom[] = {
  28. // x y z u v
  29. -(kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 0.0f, 1.0f,
  30. (kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 1.0f, 1.0f,
  31. (kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 1.0f, 0.0f,
  32. (kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 1.0f, 0.0f,
  33. -(kBottomScreenWidthNormalized / 2), 0.0f, 0.0f, 0.0f, 0.0f,
  34. -(kBottomScreenWidthNormalized / 2), -kBottomScreenHeightNormalized, 0.0f, 0.0f, 1.0f
  35. };
  36. /// RendererOpenGL constructor
  37. RendererOpenGL::RendererOpenGL() {
  38. resolution_width = std::max(VideoCore::kScreenTopWidth, VideoCore::kScreenBottomWidth);
  39. resolution_height = VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight;
  40. // Initialize screen info
  41. const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0];
  42. const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1];
  43. screen_info.Top().width = VideoCore::kScreenTopWidth;
  44. screen_info.Top().height = VideoCore::kScreenTopHeight;
  45. screen_info.Top().stride = framebuffer_top.stride;
  46. screen_info.Top().flipped_xfb_data = xfb_top_flipped;
  47. screen_info.Bottom().width = VideoCore::kScreenBottomWidth;
  48. screen_info.Bottom().height = VideoCore::kScreenBottomHeight;
  49. screen_info.Bottom().stride = framebuffer_sub.stride;
  50. screen_info.Bottom().flipped_xfb_data = xfb_bottom_flipped;
  51. }
  52. /// RendererOpenGL destructor
  53. RendererOpenGL::~RendererOpenGL() {
  54. }
  55. /// Swap buffers (render frame)
  56. void RendererOpenGL::SwapBuffers() {
  57. render_window->MakeCurrent();
  58. // EFB->XFB copy
  59. // TODO(bunnei): This is a hack and does not belong here. The copy should be triggered by some
  60. // register write.
  61. //
  62. // TODO(princesspeachum): (related to above^) this should only be called when there's new data, not every frame.
  63. // Currently this uploads data that shouldn't have changed.
  64. common::Rect framebuffer_size(0, 0, resolution_width, resolution_height);
  65. RenderXFB(framebuffer_size, framebuffer_size);
  66. // XFB->Window copy
  67. RenderFramebuffer();
  68. // Swap buffers
  69. render_window->PollEvents();
  70. render_window->SwapBuffers();
  71. }
  72. /**
  73. * Helper function to flip framebuffer from left-to-right to top-to-bottom
  74. * @param raw_data Pointer to input raw framebuffer in V/RAM
  75. * @param screen_info ScreenInfo structure with screen size and output buffer pointer
  76. * @todo Early on hack... I'd like to find a more efficient way of doing this /bunnei
  77. */
  78. void RendererOpenGL::FlipFramebuffer(const u8* raw_data, ScreenInfo& screen_info) {
  79. for (int x = 0; x < screen_info.width; x++) {
  80. int in_coord = x * screen_info.stride;
  81. for (int y = screen_info.height-1; y >= 0; y--) {
  82. // TODO: Properly support other framebuffer formats
  83. int out_coord = (x + y * screen_info.width) * 3;
  84. screen_info.flipped_xfb_data[out_coord] = raw_data[in_coord + 2]; // Red
  85. screen_info.flipped_xfb_data[out_coord + 1] = raw_data[in_coord + 1]; // Green
  86. screen_info.flipped_xfb_data[out_coord + 2] = raw_data[in_coord]; // Blue
  87. in_coord += 3;
  88. }
  89. }
  90. }
  91. /**
  92. * Renders external framebuffer (XFB)
  93. * @param src_rect Source rectangle in XFB to copy
  94. * @param dst_rect Destination rectangle in output framebuffer to copy to
  95. */
  96. void RendererOpenGL::RenderXFB(const common::Rect& src_rect, const common::Rect& dst_rect) {
  97. const auto& framebuffer_top = GPU::g_regs.framebuffer_config[0];
  98. const auto& framebuffer_sub = GPU::g_regs.framebuffer_config[1];
  99. const u32 active_fb_top = (framebuffer_top.active_fb == 1)
  100. ? Memory::PhysicalToVirtualAddress(framebuffer_top.address_left2)
  101. : Memory::PhysicalToVirtualAddress(framebuffer_top.address_left1);
  102. const u32 active_fb_sub = (framebuffer_sub.active_fb == 1)
  103. ? Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left2)
  104. : Memory::PhysicalToVirtualAddress(framebuffer_sub.address_left1);
  105. DEBUG_LOG(GPU, "RenderXFB: 0x%08x bytes from 0x%08x(%dx%d), fmt %x",
  106. framebuffer_top.stride * framebuffer_top.height,
  107. active_fb_top, (int)framebuffer_top.width,
  108. (int)framebuffer_top.height, (int)framebuffer_top.format);
  109. FlipFramebuffer(Memory::GetPointer(active_fb_top), screen_info.Top());
  110. FlipFramebuffer(Memory::GetPointer(active_fb_sub), screen_info.Bottom());
  111. for (int i = 0; i < 2; i++) {
  112. ScreenInfo* current_screen = &screen_info[i];
  113. glBindTexture(GL_TEXTURE_2D, current_screen->texture_id);
  114. // TODO: This should consider the GPU registers for framebuffer width, height and stride.
  115. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, current_screen->width, current_screen->height,
  116. GL_RGB, GL_UNSIGNED_BYTE, current_screen->flipped_xfb_data);
  117. }
  118. glBindTexture(GL_TEXTURE_2D, 0);
  119. // TODO(princesspeachum):
  120. // Only the subset src_rect of the GPU buffer
  121. // should be copied into the texture of the relevant screen.
  122. //
  123. // The method's parameters also only include src_rect and dest_rec for one screen,
  124. // so this may need to be changed (pair for each screen).
  125. }
  126. /// Initialize the FBO
  127. void RendererOpenGL::InitFramebuffer() {
  128. program_id = ShaderUtil::LoadShaders(GLShaders::g_vertex_shader, GLShaders::g_fragment_shader);
  129. sampler_id = glGetUniformLocation(program_id, "sampler");
  130. attrib_position = glGetAttribLocation(program_id, "position");
  131. attrib_texcoord = glGetAttribLocation(program_id, "texCoord");
  132. // Generate vertex buffers for both screens
  133. glGenBuffers(1, &screen_info.Top().vertex_buffer_id);
  134. glGenBuffers(1, &screen_info.Bottom().vertex_buffer_id);
  135. // Attach vertex data for top screen
  136. glBindBuffer(GL_ARRAY_BUFFER, screen_info.Top().vertex_buffer_id);
  137. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_top), g_vbuffer_top, GL_STATIC_DRAW);
  138. // Attach vertex data for bottom screen
  139. glBindBuffer(GL_ARRAY_BUFFER, screen_info.Bottom().vertex_buffer_id);
  140. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vbuffer_bottom), g_vbuffer_bottom, GL_STATIC_DRAW);
  141. // Create color buffers for both screens
  142. glGenTextures(1, &screen_info.Top().texture_id);
  143. glGenTextures(1, &screen_info.Bottom().texture_id);
  144. for (int i = 0; i < 2; i++) {
  145. ScreenInfo* current_screen = &screen_info[i];
  146. // Allocate texture
  147. glBindTexture(GL_TEXTURE_2D, current_screen->vertex_buffer_id);
  148. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, current_screen->width, current_screen->height,
  149. 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  150. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  151. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  152. }
  153. glBindTexture(GL_TEXTURE_2D, 0);
  154. }
  155. void RendererOpenGL::RenderFramebuffer() {
  156. glViewport(0, 0, resolution_width, resolution_height);
  157. glClear(GL_COLOR_BUFFER_BIT);
  158. glUseProgram(program_id);
  159. // Bind texture in Texture Unit 0
  160. glActiveTexture(GL_TEXTURE0);
  161. glEnableVertexAttribArray(attrib_position);
  162. glEnableVertexAttribArray(attrib_texcoord);
  163. for (int i = 0; i < 2; i++) {
  164. ScreenInfo* current_screen = &screen_info[i];
  165. glBindTexture(GL_TEXTURE_2D, current_screen->texture_id);
  166. // Set sampler on Texture Unit 0
  167. glUniform1i(sampler_id, 0);
  168. glBindBuffer(GL_ARRAY_BUFFER, current_screen->vertex_buffer_id);
  169. // Vertex buffer layout
  170. const GLsizei stride = 5 * sizeof(GLfloat);
  171. const GLvoid* uv_offset = (const GLvoid*)(3 * sizeof(GLfloat));
  172. // Configure vertex buffer
  173. glVertexAttribPointer(attrib_position, 3, GL_FLOAT, GL_FALSE, stride, NULL);
  174. glVertexAttribPointer(attrib_texcoord, 2, GL_FLOAT, GL_FALSE, stride, uv_offset);
  175. // Draw screen
  176. glDrawArrays(GL_TRIANGLES, 0, 6);
  177. }
  178. glDisableVertexAttribArray(attrib_position);
  179. glDisableVertexAttribArray(attrib_texcoord);
  180. m_current_frame++;
  181. }
  182. /// Updates the framerate
  183. void RendererOpenGL::UpdateFramerate() {
  184. }
  185. /**
  186. * Set the emulator window to use for renderer
  187. * @param window EmuWindow handle to emulator window to use for rendering
  188. */
  189. void RendererOpenGL::SetWindow(EmuWindow* window) {
  190. render_window = window;
  191. }
  192. /// Initialize the renderer
  193. void RendererOpenGL::Init() {
  194. render_window->MakeCurrent();
  195. int err = ogl_LoadFunctions();
  196. if (ogl_LOAD_SUCCEEDED != err) {
  197. ERROR_LOG(RENDER, "Failed to initialize GL functions! Exiting...");
  198. exit(-1);
  199. }
  200. // Generate VAO
  201. glGenVertexArrays(1, &vertex_array_id);
  202. glBindVertexArray(vertex_array_id);
  203. glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
  204. glDisable(GL_DEPTH_TEST);
  205. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  206. // Initialize everything else
  207. // --------------------------
  208. InitFramebuffer();
  209. NOTICE_LOG(RENDER, "GL_VERSION: %s\n", glGetString(GL_VERSION));
  210. }
  211. /// Shutdown the renderer
  212. void RendererOpenGL::ShutDown() {
  213. }