renderer_opengl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 display_srgb{};
  44. const Common::Rectangle<float> display_texcoords{0.0f, 0.0f, 1.0f, 1.0f};
  45. TextureInfo texture;
  46. };
  47. class RendererOpenGL final : public VideoCore::RendererBase {
  48. public:
  49. explicit RendererOpenGL(Core::TelemetrySession& telemetry_session_,
  50. Core::Frontend::EmuWindow& emu_window_,
  51. Core::Memory::Memory& cpu_memory_, Tegra::GPU& gpu_,
  52. std::unique_ptr<Core::Frontend::GraphicsContext> context_);
  53. ~RendererOpenGL() override;
  54. void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) override;
  55. VideoCore::RasterizerInterface* ReadRasterizer() override {
  56. return &rasterizer;
  57. }
  58. [[nodiscard]] std::string GetDeviceVendor() const override {
  59. return device.GetVendorName();
  60. }
  61. private:
  62. /// Initializes the OpenGL state and creates persistent objects.
  63. void InitOpenGLObjects();
  64. void AddTelemetryFields();
  65. void ConfigureFramebufferTexture(TextureInfo& texture,
  66. const Tegra::FramebufferConfig& framebuffer);
  67. /// Draws the emulated screens to the emulator window.
  68. void DrawScreen(const Layout::FramebufferLayout& layout);
  69. void RenderScreenshot();
  70. /// Loads framebuffer from emulated memory into the active OpenGL texture.
  71. void LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer);
  72. /// Fills active OpenGL texture with the given RGB color.Since the color is solid, the texture
  73. /// can be 1x1 but will stretch across whatever it's rendered on.
  74. void LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a,
  75. const TextureInfo& texture);
  76. void PrepareRendertarget(const Tegra::FramebufferConfig* framebuffer);
  77. Core::TelemetrySession& telemetry_session;
  78. Core::Frontend::EmuWindow& emu_window;
  79. Core::Memory::Memory& cpu_memory;
  80. Tegra::GPU& gpu;
  81. Device device;
  82. StateTracker state_tracker;
  83. ProgramManager program_manager;
  84. RasterizerOpenGL rasterizer;
  85. // OpenGL object IDs
  86. OGLSampler present_sampler;
  87. OGLBuffer vertex_buffer;
  88. OGLProgram present_vertex;
  89. OGLProgram present_fragment;
  90. OGLFramebuffer screenshot_framebuffer;
  91. // GPU address of the vertex buffer
  92. GLuint64EXT vertex_buffer_address = 0;
  93. /// Display information for Switch screen
  94. ScreenInfo screen_info;
  95. /// OpenGL framebuffer data
  96. std::vector<u8> gl_framebuffer_data;
  97. /// Used for transforming the framebuffer orientation
  98. Tegra::FramebufferConfig::TransformFlags framebuffer_transform_flags{};
  99. Common::Rectangle<int> framebuffer_crop_rect;
  100. };
  101. } // namespace OpenGL