renderer_opengl.cpp 20 KB

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