renderer_opengl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_resource_manager.h"
  12. #include "video_core/renderer_opengl/gl_shader_manager.h"
  13. #include "video_core/renderer_opengl/gl_state_tracker.h"
  14. namespace Core {
  15. class System;
  16. }
  17. namespace Core::Frontend {
  18. class EmuWindow;
  19. }
  20. namespace Layout {
  21. struct FramebufferLayout;
  22. }
  23. namespace OpenGL {
  24. /// Structure used for storing information about the textures for the Switch screen
  25. struct TextureInfo {
  26. OGLTexture resource;
  27. GLsizei width;
  28. GLsizei height;
  29. GLenum gl_format;
  30. GLenum gl_type;
  31. Tegra::FramebufferConfig::PixelFormat pixel_format;
  32. };
  33. /// Structure used for storing information about the display target for the Switch screen
  34. struct ScreenInfo {
  35. GLuint display_texture{};
  36. bool display_srgb{};
  37. const Common::Rectangle<float> display_texcoords{0.0f, 0.0f, 1.0f, 1.0f};
  38. TextureInfo texture;
  39. };
  40. struct PresentationTexture {
  41. u32 width = 0;
  42. u32 height = 0;
  43. OGLTexture texture;
  44. };
  45. class FrameMailbox;
  46. class RendererOpenGL final : public VideoCore::RendererBase {
  47. public:
  48. explicit RendererOpenGL(Core::Frontend::EmuWindow& emu_window, Core::System& system,
  49. Core::Frontend::GraphicsContext& context);
  50. ~RendererOpenGL() override;
  51. bool Init() override;
  52. void ShutDown() override;
  53. void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) override;
  54. bool TryPresent(int timeout_ms) override;
  55. private:
  56. /// Initializes the OpenGL state and creates persistent objects.
  57. void InitOpenGLObjects();
  58. void AddTelemetryFields();
  59. void CreateRasterizer();
  60. void ConfigureFramebufferTexture(TextureInfo& texture,
  61. const Tegra::FramebufferConfig& framebuffer);
  62. /// Draws the emulated screens to the emulator window.
  63. void DrawScreen(const Layout::FramebufferLayout& layout);
  64. void RenderScreenshot();
  65. /// Loads framebuffer from emulated memory into the active OpenGL texture.
  66. void LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer);
  67. /// Fills active OpenGL texture with the given RGB color.Since the color is solid, the texture
  68. /// can be 1x1 but will stretch across whatever it's rendered on.
  69. void LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a,
  70. const TextureInfo& texture);
  71. void PrepareRendertarget(const Tegra::FramebufferConfig* framebuffer);
  72. bool Present(int timeout_ms);
  73. Core::Frontend::EmuWindow& emu_window;
  74. Core::System& system;
  75. Core::Frontend::GraphicsContext& context;
  76. const Device device;
  77. StateTracker state_tracker{system};
  78. // OpenGL object IDs
  79. OGLBuffer vertex_buffer;
  80. OGLProgram vertex_program;
  81. OGLProgram fragment_program;
  82. OGLPipeline pipeline;
  83. OGLFramebuffer screenshot_framebuffer;
  84. // GPU address of the vertex buffer
  85. GLuint64EXT vertex_buffer_address = 0;
  86. /// Display information for Switch screen
  87. ScreenInfo screen_info;
  88. /// Global dummy shader pipeline
  89. ProgramManager program_manager;
  90. /// OpenGL framebuffer data
  91. std::vector<u8> gl_framebuffer_data;
  92. /// Used for transforming the framebuffer orientation
  93. Tegra::FramebufferConfig::TransformFlags framebuffer_transform_flags;
  94. Common::Rectangle<int> framebuffer_crop_rect;
  95. /// Frame presentation mailbox
  96. std::unique_ptr<FrameMailbox> frame_mailbox;
  97. bool has_debug_tool = false;
  98. };
  99. } // namespace OpenGL