layer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "video_core/framebuffer_config.h"
  4. #include "video_core/renderer_opengl/gl_blit_screen.h"
  5. #include "video_core/renderer_opengl/gl_rasterizer.h"
  6. #include "video_core/renderer_opengl/present/fsr.h"
  7. #include "video_core/renderer_opengl/present/fxaa.h"
  8. #include "video_core/renderer_opengl/present/layer.h"
  9. #include "video_core/renderer_opengl/present/present_uniforms.h"
  10. #include "video_core/renderer_opengl/present/smaa.h"
  11. #include "video_core/surface.h"
  12. #include "video_core/textures/decoders.h"
  13. namespace OpenGL {
  14. Layer::Layer(RasterizerOpenGL& rasterizer_, Tegra::MaxwellDeviceMemoryManager& device_memory_)
  15. : rasterizer(rasterizer_), device_memory(device_memory_) {
  16. // Allocate textures for the screen
  17. framebuffer_texture.resource.Create(GL_TEXTURE_2D);
  18. const GLuint texture = framebuffer_texture.resource.handle;
  19. glTextureStorage2D(texture, 1, GL_RGBA8, 1, 1);
  20. // Clear screen to black
  21. const u8 framebuffer_data[4] = {0, 0, 0, 0};
  22. glClearTexImage(framebuffer_texture.resource.handle, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  23. framebuffer_data);
  24. }
  25. Layer::~Layer() = default;
  26. GLuint Layer::ConfigureDraw(std::array<GLfloat, 3 * 2>& out_matrix,
  27. std::array<ScreenRectVertex, 4>& out_vertices,
  28. ProgramManager& program_manager,
  29. const Tegra::FramebufferConfig& framebuffer,
  30. const Layout::FramebufferLayout& layout) {
  31. FramebufferTextureInfo info = PrepareRenderTarget(framebuffer);
  32. auto crop = Tegra::NormalizeCrop(framebuffer, info.width, info.height);
  33. GLuint texture = info.display_texture;
  34. auto anti_aliasing = Settings::values.anti_aliasing.GetValue();
  35. if (anti_aliasing != Settings::AntiAliasing::None) {
  36. glEnablei(GL_SCISSOR_TEST, 0);
  37. auto viewport_width = Settings::values.resolution_info.ScaleUp(framebuffer_texture.width);
  38. auto viewport_height = Settings::values.resolution_info.ScaleUp(framebuffer_texture.height);
  39. glScissorIndexed(0, 0, 0, viewport_width, viewport_height);
  40. glViewportIndexedf(0, 0.0f, 0.0f, static_cast<GLfloat>(viewport_width),
  41. static_cast<GLfloat>(viewport_height));
  42. switch (anti_aliasing) {
  43. case Settings::AntiAliasing::Fxaa:
  44. CreateFXAA();
  45. texture = fxaa->Draw(program_manager, info.display_texture);
  46. break;
  47. case Settings::AntiAliasing::Smaa:
  48. default:
  49. CreateSMAA();
  50. texture = smaa->Draw(program_manager, info.display_texture);
  51. break;
  52. }
  53. }
  54. glDisablei(GL_SCISSOR_TEST, 0);
  55. if (Settings::values.scaling_filter.GetValue() == Settings::ScalingFilter::Fsr) {
  56. if (!fsr || fsr->NeedsRecreation(layout.screen)) {
  57. fsr = std::make_unique<FSR>(layout.screen.GetWidth(), layout.screen.GetHeight());
  58. }
  59. texture = fsr->Draw(program_manager, texture, info.scaled_width, info.scaled_height, crop);
  60. crop = {0, 0, 1, 1};
  61. }
  62. out_matrix =
  63. MakeOrthographicMatrix(static_cast<float>(layout.width), static_cast<float>(layout.height));
  64. // Map the coordinates to the screen.
  65. const auto& screen = layout.screen;
  66. const auto x = screen.left;
  67. const auto y = screen.top;
  68. const auto w = screen.GetWidth();
  69. const auto h = screen.GetHeight();
  70. out_vertices[0] = ScreenRectVertex(x, y, crop.left, crop.top);
  71. out_vertices[1] = ScreenRectVertex(x + w, y, crop.right, crop.top);
  72. out_vertices[2] = ScreenRectVertex(x, y + h, crop.left, crop.bottom);
  73. out_vertices[3] = ScreenRectVertex(x + w, y + h, crop.right, crop.bottom);
  74. return texture;
  75. }
  76. FramebufferTextureInfo Layer::PrepareRenderTarget(const Tegra::FramebufferConfig& framebuffer) {
  77. // If framebuffer is provided, reload it from memory to a texture
  78. if (framebuffer_texture.width != static_cast<GLsizei>(framebuffer.width) ||
  79. framebuffer_texture.height != static_cast<GLsizei>(framebuffer.height) ||
  80. framebuffer_texture.pixel_format != framebuffer.pixel_format ||
  81. gl_framebuffer_data.empty()) {
  82. // Reallocate texture if the framebuffer size has changed.
  83. // This is expected to not happen very often and hence should not be a
  84. // performance problem.
  85. ConfigureFramebufferTexture(framebuffer);
  86. }
  87. // Load the framebuffer from memory if needed
  88. return LoadFBToScreenInfo(framebuffer);
  89. }
  90. FramebufferTextureInfo Layer::LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer) {
  91. const VAddr framebuffer_addr{framebuffer.address + framebuffer.offset};
  92. const auto accelerated_info =
  93. rasterizer.AccelerateDisplay(framebuffer, framebuffer_addr, framebuffer.stride);
  94. if (accelerated_info) {
  95. return *accelerated_info;
  96. }
  97. // Reset the screen info's display texture to its own permanent texture
  98. FramebufferTextureInfo info{};
  99. info.display_texture = framebuffer_texture.resource.handle;
  100. info.width = framebuffer.width;
  101. info.height = framebuffer.height;
  102. info.scaled_width = framebuffer.width;
  103. info.scaled_height = framebuffer.height;
  104. // TODO(Rodrigo): Read this from HLE
  105. constexpr u32 block_height_log2 = 4;
  106. const auto pixel_format{
  107. VideoCore::Surface::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format)};
  108. const u32 bytes_per_pixel{VideoCore::Surface::BytesPerBlock(pixel_format)};
  109. const u64 size_in_bytes{Tegra::Texture::CalculateSize(
  110. true, bytes_per_pixel, framebuffer.stride, framebuffer.height, 1, block_height_log2, 0)};
  111. const u8* const host_ptr{device_memory.GetPointer<u8>(framebuffer_addr)};
  112. const std::span<const u8> input_data(host_ptr, size_in_bytes);
  113. Tegra::Texture::UnswizzleTexture(gl_framebuffer_data, input_data, bytes_per_pixel,
  114. framebuffer.width, framebuffer.height, 1, block_height_log2,
  115. 0);
  116. glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  117. glPixelStorei(GL_UNPACK_ROW_LENGTH, static_cast<GLint>(framebuffer.stride));
  118. // Update existing texture
  119. // TODO: Test what happens on hardware when you change the framebuffer dimensions so that
  120. // they differ from the LCD resolution.
  121. // TODO: Applications could theoretically crash yuzu here by specifying too large
  122. // framebuffer sizes. We should make sure that this cannot happen.
  123. glTextureSubImage2D(framebuffer_texture.resource.handle, 0, 0, 0, framebuffer.width,
  124. framebuffer.height, framebuffer_texture.gl_format,
  125. framebuffer_texture.gl_type, gl_framebuffer_data.data());
  126. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  127. return info;
  128. }
  129. void Layer::ConfigureFramebufferTexture(const Tegra::FramebufferConfig& framebuffer) {
  130. framebuffer_texture.width = framebuffer.width;
  131. framebuffer_texture.height = framebuffer.height;
  132. framebuffer_texture.pixel_format = framebuffer.pixel_format;
  133. const auto pixel_format{
  134. VideoCore::Surface::PixelFormatFromGPUPixelFormat(framebuffer.pixel_format)};
  135. const u32 bytes_per_pixel{VideoCore::Surface::BytesPerBlock(pixel_format)};
  136. gl_framebuffer_data.resize(framebuffer_texture.width * framebuffer_texture.height *
  137. bytes_per_pixel);
  138. GLint internal_format;
  139. switch (framebuffer.pixel_format) {
  140. case Service::android::PixelFormat::Rgba8888:
  141. internal_format = GL_RGBA8;
  142. framebuffer_texture.gl_format = GL_RGBA;
  143. framebuffer_texture.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  144. break;
  145. case Service::android::PixelFormat::Rgb565:
  146. internal_format = GL_RGB565;
  147. framebuffer_texture.gl_format = GL_RGB;
  148. framebuffer_texture.gl_type = GL_UNSIGNED_SHORT_5_6_5;
  149. break;
  150. default:
  151. internal_format = GL_RGBA8;
  152. framebuffer_texture.gl_format = GL_RGBA;
  153. framebuffer_texture.gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
  154. // UNIMPLEMENTED_MSG("Unknown framebuffer pixel format: {}",
  155. // static_cast<u32>(framebuffer.pixel_format));
  156. break;
  157. }
  158. framebuffer_texture.resource.Release();
  159. framebuffer_texture.resource.Create(GL_TEXTURE_2D);
  160. glTextureStorage2D(framebuffer_texture.resource.handle, 1, internal_format,
  161. framebuffer_texture.width, framebuffer_texture.height);
  162. fxaa.reset();
  163. smaa.reset();
  164. }
  165. void Layer::CreateFXAA() {
  166. smaa.reset();
  167. if (!fxaa) {
  168. fxaa = std::make_unique<FXAA>(
  169. Settings::values.resolution_info.ScaleUp(framebuffer_texture.width),
  170. Settings::values.resolution_info.ScaleUp(framebuffer_texture.height));
  171. }
  172. }
  173. void Layer::CreateSMAA() {
  174. fxaa.reset();
  175. if (!smaa) {
  176. smaa = std::make_unique<SMAA>(
  177. Settings::values.resolution_info.ScaleUp(framebuffer_texture.width),
  178. Settings::values.resolution_info.ScaleUp(framebuffer_texture.height));
  179. }
  180. }
  181. } // namespace OpenGL