renderer_opengl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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::System& system, Core::Frontend::EmuWindow& emu_window,
  49. Tegra::GPU& gpu,
  50. std::unique_ptr<Core::Frontend::GraphicsContext> context);
  51. ~RendererOpenGL() override;
  52. bool Init() override;
  53. void ShutDown() override;
  54. void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) override;
  55. bool TryPresent(int timeout_ms) override;
  56. private:
  57. /// Initializes the OpenGL state and creates persistent objects.
  58. void InitOpenGLObjects();
  59. void AddTelemetryFields();
  60. void CreateRasterizer();
  61. void ConfigureFramebufferTexture(TextureInfo& texture,
  62. const Tegra::FramebufferConfig& framebuffer);
  63. /// Draws the emulated screens to the emulator window.
  64. void DrawScreen(const Layout::FramebufferLayout& layout);
  65. void RenderScreenshot();
  66. /// Loads framebuffer from emulated memory into the active OpenGL texture.
  67. void LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer);
  68. /// Fills active OpenGL texture with the given RGB color.Since the color is solid, the texture
  69. /// can be 1x1 but will stretch across whatever it's rendered on.
  70. void LoadColorToActiveGLTexture(u8 color_r, u8 color_g, u8 color_b, u8 color_a,
  71. const TextureInfo& texture);
  72. void PrepareRendertarget(const Tegra::FramebufferConfig* framebuffer);
  73. bool Present(int timeout_ms);
  74. Core::System& system;
  75. Core::Frontend::EmuWindow& emu_window;
  76. Tegra::GPU& gpu;
  77. const Device device;
  78. StateTracker state_tracker{system};
  79. // OpenGL object IDs
  80. OGLBuffer vertex_buffer;
  81. OGLProgram vertex_program;
  82. OGLProgram fragment_program;
  83. OGLPipeline pipeline;
  84. OGLFramebuffer screenshot_framebuffer;
  85. // GPU address of the vertex buffer
  86. GLuint64EXT vertex_buffer_address = 0;
  87. /// Display information for Switch screen
  88. ScreenInfo screen_info;
  89. /// Global dummy shader pipeline
  90. ProgramManager program_manager;
  91. /// OpenGL framebuffer data
  92. std::vector<u8> gl_framebuffer_data;
  93. /// Used for transforming the framebuffer orientation
  94. Tegra::FramebufferConfig::TransformFlags framebuffer_transform_flags{};
  95. Common::Rectangle<int> framebuffer_crop_rect;
  96. /// Frame presentation mailbox
  97. std::unique_ptr<FrameMailbox> frame_mailbox;
  98. bool has_debug_tool = false;
  99. };
  100. } // namespace OpenGL