renderer_opengl.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstddef>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <memory>
  9. #include <glad/glad.h>
  10. #include "common/assert.h"
  11. #include "common/logging/log.h"
  12. #include "common/microprofile.h"
  13. #include "common/settings.h"
  14. #include "common/telemetry.h"
  15. #include "core/core.h"
  16. #include "core/core_timing.h"
  17. #include "core/frontend/emu_window.h"
  18. #include "core/memory.h"
  19. #include "core/perf_stats.h"
  20. #include "core/telemetry_session.h"
  21. #include "video_core/host_shaders/opengl_present_frag.h"
  22. #include "video_core/host_shaders/opengl_present_vert.h"
  23. #include "video_core/renderer_opengl/gl_rasterizer.h"
  24. #include "video_core/renderer_opengl/gl_shader_manager.h"
  25. #include "video_core/renderer_opengl/gl_shader_util.h"
  26. #include "video_core/renderer_opengl/renderer_opengl.h"
  27. #include "video_core/textures/decoders.h"
  28. namespace OpenGL {
  29. namespace {
  30. constexpr GLint PositionLocation = 0;
  31. constexpr GLint TexCoordLocation = 1;
  32. constexpr GLint ModelViewMatrixLocation = 0;
  33. struct ScreenRectVertex {
  34. constexpr ScreenRectVertex(u32 x, u32 y, GLfloat u, GLfloat v)
  35. : position{{static_cast<GLfloat>(x), static_cast<GLfloat>(y)}}, tex_coord{{u, v}} {}
  36. std::array<GLfloat, 2> position;
  37. std::array<GLfloat, 2> tex_coord;
  38. };
  39. /**
  40. * Defines a 1:1 pixel ortographic projection matrix with (0,0) on the top-left
  41. * corner and (width, height) on the lower-bottom.
  42. *
  43. * The projection part of the matrix is trivial, hence these operations are represented
  44. * by a 3x2 matrix.
  45. */
  46. std::array<GLfloat, 3 * 2> MakeOrthographicMatrix(float width, float height) {
  47. std::array<GLfloat, 3 * 2> matrix; // Laid out in column-major order
  48. // clang-format off
  49. matrix[0] = 2.f / width; matrix[2] = 0.f; matrix[4] = -1.f;
  50. matrix[1] = 0.f; matrix[3] = -2.f / height; matrix[5] = 1.f;
  51. // Last matrix row is implicitly assumed to be [0, 0, 1].
  52. // clang-format on
  53. return matrix;
  54. }
  55. const char* GetSource(GLenum source) {
  56. switch (source) {
  57. case GL_DEBUG_SOURCE_API:
  58. return "API";
  59. case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
  60. return "WINDOW_SYSTEM";
  61. case GL_DEBUG_SOURCE_SHADER_COMPILER:
  62. return "SHADER_COMPILER";
  63. case GL_DEBUG_SOURCE_THIRD_PARTY:
  64. return "THIRD_PARTY";
  65. case GL_DEBUG_SOURCE_APPLICATION:
  66. return "APPLICATION";
  67. case GL_DEBUG_SOURCE_OTHER:
  68. return "OTHER";
  69. default:
  70. UNREACHABLE();
  71. return "Unknown source";
  72. }
  73. }
  74. const char* GetType(GLenum type) {
  75. switch (type) {
  76. case GL_DEBUG_TYPE_ERROR:
  77. return "ERROR";
  78. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
  79. return "DEPRECATED_BEHAVIOR";
  80. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
  81. return "UNDEFINED_BEHAVIOR";
  82. case GL_DEBUG_TYPE_PORTABILITY:
  83. return "PORTABILITY";
  84. case GL_DEBUG_TYPE_PERFORMANCE:
  85. return "PERFORMANCE";
  86. case GL_DEBUG_TYPE_OTHER:
  87. return "OTHER";
  88. case GL_DEBUG_TYPE_MARKER:
  89. return "MARKER";
  90. default:
  91. UNREACHABLE();
  92. return "Unknown type";
  93. }
  94. }
  95. void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
  96. const GLchar* message, const void* user_param) {
  97. const char format[] = "{} {} {}: {}";
  98. const char* const str_source = GetSource(source);
  99. const char* const str_type = GetType(type);
  100. switch (severity) {
  101. case GL_DEBUG_SEVERITY_HIGH:
  102. LOG_CRITICAL(Render_OpenGL, format, str_source, str_type, id, message);
  103. break;
  104. case GL_DEBUG_SEVERITY_MEDIUM:
  105. LOG_WARNING(Render_OpenGL, format, str_source, str_type, id, message);
  106. break;
  107. case GL_DEBUG_SEVERITY_NOTIFICATION:
  108. case GL_DEBUG_SEVERITY_LOW:
  109. LOG_DEBUG(Render_OpenGL, format, str_source, str_type, id, message);
  110. break;
  111. }
  112. }
  113. } // Anonymous namespace
  114. RendererOpenGL::RendererOpenGL(Core::TelemetrySession& telemetry_session_,
  115. Core::Frontend::EmuWindow& emu_window_,
  116. Core::Memory::Memory& cpu_memory_, Tegra::GPU& gpu_,
  117. std::unique_ptr<Core::Frontend::GraphicsContext> context_)
  118. : RendererBase{emu_window_, std::move(context_)}, telemetry_session{telemetry_session_},
  119. emu_window{emu_window_}, cpu_memory{cpu_memory_}, gpu{gpu_}, state_tracker{gpu},
  120. program_manager{device},
  121. rasterizer(emu_window, gpu, cpu_memory, device, screen_info, program_manager, state_tracker) {
  122. if (Settings::values.renderer_debug && GLAD_GL_KHR_debug) {
  123. glEnable(GL_DEBUG_OUTPUT);
  124. glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
  125. glDebugMessageCallback(DebugHandler, nullptr);
  126. }
  127. AddTelemetryFields();
  128. InitOpenGLObjects();
  129. // Initialize default attributes to match hardware's disabled attributes
  130. GLint max_attribs{};
  131. glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_attribs);
  132. for (GLint attrib = 0; attrib < max_attribs; ++attrib) {
  133. glVertexAttrib4f(attrib, 0.0f, 0.0f, 0.0f, 0.0f);
  134. }
  135. // Enable seamless cubemaps when per texture parameters are not available
  136. if (!GLAD_GL_ARB_seamless_cubemap_per_texture && !GLAD_GL_AMD_seamless_cubemap_per_texture) {
  137. glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  138. }
  139. // Enable unified vertex attributes and query vertex buffer address when the driver supports it
  140. if (device.HasVertexBufferUnifiedMemory()) {
  141. glEnableClientState(GL_VERTEX_ATTRIB_ARRAY_UNIFIED_NV);
  142. glEnableClientState(GL_ELEMENT_ARRAY_UNIFIED_NV);
  143. glMakeNamedBufferResidentNV(vertex_buffer.handle, GL_READ_ONLY);
  144. glGetNamedBufferParameterui64vNV(vertex_buffer.handle, GL_BUFFER_GPU_ADDRESS_NV,
  145. &vertex_buffer_address);
  146. }
  147. }
  148. RendererOpenGL::~RendererOpenGL() = default;
  149. void RendererOpenGL::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) {
  150. if (!framebuffer) {
  151. return;
  152. }
  153. PrepareRendertarget(framebuffer);
  154. RenderScreenshot();
  155. state_tracker.BindFramebuffer(0);
  156. DrawScreen(emu_window.GetFramebufferLayout());
  157. ++m_current_frame;
  158. gpu.RendererFrameEndNotify();
  159. rasterizer.TickFrame();
  160. context->SwapBuffers();
  161. render_window.OnFrameDisplayed();
  162. }
  163. void RendererOpenGL::PrepareRendertarget(const Tegra::FramebufferConfig* framebuffer) {
  164. if (!framebuffer) {
  165. return;
  166. }
  167. // If framebuffer is provided, reload it from memory to a texture
  168. if (screen_info.texture.width != static_cast<GLsizei>(framebuffer->width) ||
  169. screen_info.texture.height != static_cast<GLsizei>(framebuffer->height) ||
  170. screen_info.texture.pixel_format != framebuffer->pixel_format ||
  171. gl_framebuffer_data.empty()) {
  172. // Reallocate texture if the framebuffer size has changed.
  173. // This is expected to not happen very often and hence should not be a
  174. // performance problem.
  175. ConfigureFramebufferTexture(screen_info.texture, *framebuffer);
  176. }
  177. // Load the framebuffer from memory, draw it to the screen, and swap buffers
  178. LoadFBToScreenInfo(*framebuffer);
  179. }
  180. void RendererOpenGL::LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer) {
  181. // Framebuffer orientation handling
  182. framebuffer_transform_flags = framebuffer.transform_flags;
  183. framebuffer_crop_rect = framebuffer.crop_rect;
  184. const VAddr framebuffer_addr{framebuffer.address + framebuffer.offset};
  185. if (rasterizer.AccelerateDisplay(framebuffer, framebuffer_addr, framebuffer.stride)) {
  186. return;
  187. }
  188. // Reset the screen info's display texture to its own permanent texture
  189. screen_info.display_texture = screen_info.texture.resource.handle;
  190. // TODO(Rodrigo): Read this from HLE
  191. constexpr u32 block_height_log2 = 4;
  192. const auto pixel_format{
  193. VideoCore::Surface::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format)};
  194. const u32 bytes_per_pixel{VideoCore::Surface::BytesPerBlock(pixel_format)};
  195. const u64 size_in_bytes{Tegra::Texture::CalculateSize(
  196. true, bytes_per_pixel, framebuffer.stride, framebuffer.height, 1, block_height_log2, 0)};
  197. const u8* const host_ptr{cpu_memory.GetPointer(framebuffer_addr)};
  198. const std::span<const u8> input_data(host_ptr, size_in_bytes);
  199. Tegra::Texture::UnswizzleTexture(gl_framebuffer_data, input_data, bytes_per_pixel,
  200. framebuffer.width, framebuffer.height, 1, block_height_log2,
  201. 0);
  202. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  203. glPixelStorei(GL_UNPACK_ROW_LENGTH, static_cast<GLint>(framebuffer.stride));
  204. // Update existing texture
  205. // TODO: Test what happens on hardware when you change the framebuffer dimensions so that
  206. // they differ from the LCD resolution.
  207. // TODO: Applications could theoretically crash yuzu here by specifying too large
  208. // framebuffer sizes. We should make sure that this cannot happen.
  209. glTextureSubImage2D(screen_info.texture.resource.handle, 0, 0, 0, framebuffer.width,
  210. framebuffer.height, screen_info.texture.gl_format,
  211. screen_info.texture.gl_type, gl_framebuffer_data.data());
  212. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  213. }
  214. void RendererOpenGL::LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a,
  215. const TextureInfo& texture) {
  216. const u8 framebuffer_data[4] = {color_a, color_b, color_g, color_r};
  217. glClearTexImage(texture.resource.handle, 0, GL_RGBA, GL_UNSIGNED_BYTE, framebuffer_data);
  218. }
  219. void RendererOpenGL::InitOpenGLObjects() {
  220. // Create shader programs
  221. present_vertex = CreateProgram(HostShaders::OPENGL_PRESENT_VERT, GL_VERTEX_SHADER);
  222. present_fragment = CreateProgram(HostShaders::OPENGL_PRESENT_FRAG, GL_FRAGMENT_SHADER);
  223. // Generate presentation sampler
  224. present_sampler.Create();
  225. glSamplerParameteri(present_sampler.handle, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  226. // Generate VBO handle for drawing
  227. vertex_buffer.Create();
  228. // Attach vertex data to VAO
  229. glNamedBufferData(vertex_buffer.handle, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW);
  230. // Allocate textures for the screen
  231. screen_info.texture.resource.Create(GL_TEXTURE_2D);
  232. const GLuint texture = screen_info.texture.resource.handle;
  233. glTextureStorage2D(texture, 1, GL_RGBA8, 1, 1);
  234. screen_info.display_texture = screen_info.texture.resource.handle;
  235. // Clear screen to black
  236. LoadColorToActiveGLTexture(0, 0, 0, 0, screen_info.texture);
  237. }
  238. void RendererOpenGL::AddTelemetryFields() {
  239. const char* const gl_version{reinterpret_cast<char const*>(glGetString(GL_VERSION))};
  240. const char* const gpu_vendor{reinterpret_cast<char const*>(glGetString(GL_VENDOR))};
  241. const char* const gpu_model{reinterpret_cast<char const*>(glGetString(GL_RENDERER))};
  242. LOG_INFO(Render_OpenGL, "GL_VERSION: {}", gl_version);
  243. LOG_INFO(Render_OpenGL, "GL_VENDOR: {}", gpu_vendor);
  244. LOG_INFO(Render_OpenGL, "GL_RENDERER: {}", gpu_model);
  245. constexpr auto user_system = Common::Telemetry::FieldType::UserSystem;
  246. telemetry_session.AddField(user_system, "GPU_Vendor", std::string(gpu_vendor));
  247. telemetry_session.AddField(user_system, "GPU_Model", std::string(gpu_model));
  248. telemetry_session.AddField(user_system, "GPU_OpenGL_Version", std::string(gl_version));
  249. }
  250. void RendererOpenGL::ConfigureFramebufferTexture(TextureInfo& texture,
  251. const Tegra::FramebufferConfig& framebuffer) {
  252. texture.width = framebuffer.width;
  253. texture.height = framebuffer.height;
  254. texture.pixel_format = framebuffer.pixel_format;
  255. const auto pixel_format{
  256. VideoCore::Surface::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format)};
  257. const u32 bytes_per_pixel{VideoCore::Surface::BytesPerBlock(pixel_format)};
  258. gl_framebuffer_data.resize(texture.width * texture.height * bytes_per_pixel);
  259. GLint internal_format;
  260. switch (framebuffer.pixel_format) {
  261. case Tegra::FramebufferConfig::PixelFormat::A8B8G8R8_UNORM:
  262. internal_format = GL_RGBA8;
  263. texture.gl_format = GL_RGBA;
  264. texture.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  265. break;
  266. case Tegra::FramebufferConfig::PixelFormat::RGB565_UNORM:
  267. internal_format = GL_RGB565;
  268. texture.gl_format = GL_RGB;
  269. texture.gl_type = GL_UNSIGNED_SHORT_5_6_5;
  270. break;
  271. default:
  272. internal_format = GL_RGBA8;
  273. texture.gl_format = GL_RGBA;
  274. texture.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  275. // UNIMPLEMENTED_MSG("Unknown framebuffer pixel format: {}",
  276. // static_cast<u32>(framebuffer.pixel_format));
  277. }
  278. texture.resource.Release();
  279. texture.resource.Create(GL_TEXTURE_2D);
  280. glTextureStorage2D(texture.resource.handle, 1, internal_format, texture.width, texture.height);
  281. }
  282. void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) {
  283. if (renderer_settings.set_background_color) {
  284. // Update background color before drawing
  285. glClearColor(Settings::values.bg_red.GetValue() / 255.0f,
  286. Settings::values.bg_green.GetValue() / 255.0f,
  287. Settings::values.bg_blue.GetValue() / 255.0f, 1.0f);
  288. }
  289. // Set projection matrix
  290. const std::array ortho_matrix =
  291. MakeOrthographicMatrix(static_cast<float>(layout.width), static_cast<float>(layout.height));
  292. program_manager.BindPresentPrograms(present_vertex.handle, present_fragment.handle);
  293. glProgramUniformMatrix3x2fv(present_vertex.handle, ModelViewMatrixLocation, 1, GL_FALSE,
  294. ortho_matrix.data());
  295. const auto& texcoords = screen_info.display_texcoords;
  296. auto left = texcoords.left;
  297. auto right = texcoords.right;
  298. if (framebuffer_transform_flags != Tegra::FramebufferConfig::TransformFlags::Unset) {
  299. if (framebuffer_transform_flags == Tegra::FramebufferConfig::TransformFlags::FlipV) {
  300. // Flip the framebuffer vertically
  301. left = texcoords.right;
  302. right = texcoords.left;
  303. } else {
  304. // Other transformations are unsupported
  305. LOG_CRITICAL(Render_OpenGL, "Unsupported framebuffer_transform_flags={}",
  306. framebuffer_transform_flags);
  307. UNIMPLEMENTED();
  308. }
  309. }
  310. ASSERT_MSG(framebuffer_crop_rect.top == 0, "Unimplemented");
  311. ASSERT_MSG(framebuffer_crop_rect.left == 0, "Unimplemented");
  312. // Scale the output by the crop width/height. This is commonly used with 1280x720 rendering
  313. // (e.g. handheld mode) on a 1920x1080 framebuffer.
  314. f32 scale_u = 1.f, scale_v = 1.f;
  315. if (framebuffer_crop_rect.GetWidth() > 0) {
  316. scale_u = static_cast<f32>(framebuffer_crop_rect.GetWidth()) /
  317. static_cast<f32>(screen_info.texture.width);
  318. }
  319. if (framebuffer_crop_rect.GetHeight() > 0) {
  320. scale_v = static_cast<f32>(framebuffer_crop_rect.GetHeight()) /
  321. static_cast<f32>(screen_info.texture.height);
  322. }
  323. const auto& screen = layout.screen;
  324. const std::array vertices = {
  325. ScreenRectVertex(screen.left, screen.top, texcoords.top * scale_u, left * scale_v),
  326. ScreenRectVertex(screen.right, screen.top, texcoords.bottom * scale_u, left * scale_v),
  327. ScreenRectVertex(screen.left, screen.bottom, texcoords.top * scale_u, right * scale_v),
  328. ScreenRectVertex(screen.right, screen.bottom, texcoords.bottom * scale_u, right * scale_v),
  329. };
  330. glNamedBufferSubData(vertex_buffer.handle, 0, sizeof(vertices), std::data(vertices));
  331. // TODO: Signal state tracker about these changes
  332. state_tracker.NotifyScreenDrawVertexArray();
  333. state_tracker.NotifyPolygonModes();
  334. state_tracker.NotifyViewport0();
  335. state_tracker.NotifyScissor0();
  336. state_tracker.NotifyColorMask(0);
  337. state_tracker.NotifyBlend0();
  338. state_tracker.NotifyFramebuffer();
  339. state_tracker.NotifyFrontFace();
  340. state_tracker.NotifyCullTest();
  341. state_tracker.NotifyDepthTest();
  342. state_tracker.NotifyStencilTest();
  343. state_tracker.NotifyPolygonOffset();
  344. state_tracker.NotifyRasterizeEnable();
  345. state_tracker.NotifyFramebufferSRGB();
  346. state_tracker.NotifyLogicOp();
  347. state_tracker.NotifyClipControl();
  348. state_tracker.NotifyAlphaTest();
  349. state_tracker.ClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
  350. glEnable(GL_CULL_FACE);
  351. if (screen_info.display_srgb) {
  352. glEnable(GL_FRAMEBUFFER_SRGB);
  353. } else {
  354. glDisable(GL_FRAMEBUFFER_SRGB);
  355. }
  356. glDisable(GL_COLOR_LOGIC_OP);
  357. glDisable(GL_DEPTH_TEST);
  358. glDisable(GL_STENCIL_TEST);
  359. glDisable(GL_POLYGON_OFFSET_FILL);
  360. glDisable(GL_RASTERIZER_DISCARD);
  361. glDisable(GL_ALPHA_TEST);
  362. glDisablei(GL_BLEND, 0);
  363. glDisablei(GL_SCISSOR_TEST, 0);
  364. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  365. glCullFace(GL_BACK);
  366. glFrontFace(GL_CW);
  367. glColorMaski(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  368. glViewportIndexedf(0, 0.0f, 0.0f, static_cast<GLfloat>(layout.width),
  369. static_cast<GLfloat>(layout.height));
  370. glDepthRangeIndexed(0, 0.0, 0.0);
  371. glEnableVertexAttribArray(PositionLocation);
  372. glEnableVertexAttribArray(TexCoordLocation);
  373. glVertexAttribDivisor(PositionLocation, 0);
  374. glVertexAttribDivisor(TexCoordLocation, 0);
  375. glVertexAttribFormat(PositionLocation, 2, GL_FLOAT, GL_FALSE,
  376. offsetof(ScreenRectVertex, position));
  377. glVertexAttribFormat(TexCoordLocation, 2, GL_FLOAT, GL_FALSE,
  378. offsetof(ScreenRectVertex, tex_coord));
  379. glVertexAttribBinding(PositionLocation, 0);
  380. glVertexAttribBinding(TexCoordLocation, 0);
  381. if (device.HasVertexBufferUnifiedMemory()) {
  382. glBindVertexBuffer(0, 0, 0, sizeof(ScreenRectVertex));
  383. glBufferAddressRangeNV(GL_VERTEX_ATTRIB_ARRAY_ADDRESS_NV, 0, vertex_buffer_address,
  384. sizeof(vertices));
  385. } else {
  386. glBindVertexBuffer(0, vertex_buffer.handle, 0, sizeof(ScreenRectVertex));
  387. }
  388. glBindTextureUnit(0, screen_info.display_texture);
  389. glBindSampler(0, present_sampler.handle);
  390. glClear(GL_COLOR_BUFFER_BIT);
  391. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  392. // TODO
  393. // program_manager.RestoreGuestPipeline();
  394. }
  395. void RendererOpenGL::RenderScreenshot() {
  396. if (!renderer_settings.screenshot_requested) {
  397. return;
  398. }
  399. GLint old_read_fb;
  400. GLint old_draw_fb;
  401. glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &old_read_fb);
  402. glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &old_draw_fb);
  403. // Draw the current frame to the screenshot framebuffer
  404. screenshot_framebuffer.Create();
  405. glBindFramebuffer(GL_FRAMEBUFFER, screenshot_framebuffer.handle);
  406. const Layout::FramebufferLayout layout{renderer_settings.screenshot_framebuffer_layout};
  407. GLuint renderbuffer;
  408. glGenRenderbuffers(1, &renderbuffer);
  409. glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
  410. glRenderbufferStorage(GL_RENDERBUFFER, screen_info.display_srgb ? GL_SRGB8 : GL_RGB8,
  411. layout.width, layout.height);
  412. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
  413. DrawScreen(layout);
  414. glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
  415. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  416. glReadPixels(0, 0, layout.width, layout.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
  417. renderer_settings.screenshot_bits);
  418. screenshot_framebuffer.Release();
  419. glDeleteRenderbuffers(1, &renderbuffer);
  420. glBindFramebuffer(GL_READ_FRAMEBUFFER, old_read_fb);
  421. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, old_draw_fb);
  422. renderer_settings.screenshot_complete_callback();
  423. renderer_settings.screenshot_requested = false;
  424. }
  425. } // namespace OpenGL