renderer_opengl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <vector>
  6. #include <glad/glad.h>
  7. #include "common/common_types.h"
  8. #include "common/math_util.h"
  9. #include "video_core/renderer_base.h"
  10. #include "video_core/renderer_opengl/gl_device.h"
  11. #include "video_core/renderer_opengl/gl_rasterizer.h"
  12. #include "video_core/renderer_opengl/gl_resource_manager.h"
  13. #include "video_core/renderer_opengl/gl_state_tracker.h"
  14. namespace Core {
  15. class System;
  16. class TelemetrySession;
  17. } // namespace Core
  18. namespace Core::Frontend {
  19. class EmuWindow;
  20. }
  21. namespace Core::Memory {
  22. class Memory;
  23. }
  24. namespace Layout {
  25. struct FramebufferLayout;
  26. }
  27. namespace Tegra {
  28. class GPU;
  29. }
  30. namespace OpenGL {
  31. /// Structure used for storing information about the textures for the Switch screen
  32. struct TextureInfo {
  33. OGLTexture resource;
  34. GLsizei width;
  35. GLsizei height;
  36. GLenum gl_format;
  37. GLenum gl_type;
  38. Tegra::FramebufferConfig::PixelFormat pixel_format;
  39. };
  40. /// Structure used for storing information about the display target for the Switch screen
  41. struct ScreenInfo {
  42. GLuint display_texture{};
  43. bool was_accelerated = false;
  44. bool display_srgb{};
  45. const Common::Rectangle<float> display_texcoords{0.0f, 0.0f, 1.0f, 1.0f};
  46. TextureInfo texture;
  47. };
  48. class RendererOpenGL final : public VideoCore::RendererBase {
  49. public:
  50. explicit RendererOpenGL(Core::TelemetrySession& telemetry_session_,
  51. Core::Frontend::EmuWindow& emu_window_,
  52. Core::Memory::Memory& cpu_memory_, Tegra::GPU& gpu_,
  53. std::unique_ptr<Core::Frontend::GraphicsContext> context_);
  54. ~RendererOpenGL() override;
  55. void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) override;
  56. VideoCore::RasterizerInterface* ReadRasterizer() override {
  57. return &rasterizer;
  58. }
  59. [[nodiscard]] std::string GetDeviceVendor() const override {
  60. return device.GetVendorName();
  61. }
  62. private:
  63. /// Initializes the OpenGL state and creates persistent objects.
  64. void InitOpenGLObjects();
  65. void AddTelemetryFields();
  66. void ConfigureFramebufferTexture(TextureInfo& texture,
  67. const Tegra::FramebufferConfig& framebuffer);
  68. /// Draws the emulated screens to the emulator window.
  69. void DrawScreen(const Layout::FramebufferLayout& layout);
  70. void RenderScreenshot();
  71. /// Loads framebuffer from emulated memory into the active OpenGL texture.
  72. void LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer);
  73. /// Fills active OpenGL texture with the given RGB color.Since the color is solid, the texture
  74. /// can be 1x1 but will stretch across whatever it's rendered on.
  75. void LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a,
  76. const TextureInfo& texture);
  77. void PrepareRendertarget(const Tegra::FramebufferConfig* framebuffer);
  78. Core::TelemetrySession& telemetry_session;
  79. Core::Frontend::EmuWindow& emu_window;
  80. Core::Memory::Memory& cpu_memory;
  81. Tegra::GPU& gpu;
  82. Device device;
  83. StateTracker state_tracker;
  84. ProgramManager program_manager;
  85. RasterizerOpenGL rasterizer;
  86. // OpenGL object IDs
  87. OGLSampler present_sampler;
  88. OGLSampler present_sampler_nn;
  89. OGLBuffer vertex_buffer;
  90. OGLProgram fxaa_vertex;
  91. OGLProgram fxaa_fragment;
  92. OGLProgram present_vertex;
  93. OGLProgram present_bilinear_fragment;
  94. OGLProgram present_bicubic_fragment;
  95. OGLProgram present_gaussian_fragment;
  96. OGLProgram present_scaleforce_fragment;
  97. OGLFramebuffer screenshot_framebuffer;
  98. // GPU address of the vertex buffer
  99. GLuint64EXT vertex_buffer_address = 0;
  100. /// Display information for Switch screen
  101. ScreenInfo screen_info;
  102. OGLTexture fxaa_texture;
  103. OGLFramebuffer fxaa_framebuffer;
  104. /// OpenGL framebuffer data
  105. std::vector<u8> gl_framebuffer_data;
  106. /// Used for transforming the framebuffer orientation
  107. Tegra::FramebufferConfig::TransformFlags framebuffer_transform_flags{};
  108. Common::Rectangle<int> framebuffer_crop_rect;
  109. };
  110. } // namespace OpenGL