gl_rasterizer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <cstddef>
  8. #include <map>
  9. #include <memory>
  10. #include <optional>
  11. #include <tuple>
  12. #include <utility>
  13. #include <boost/icl/interval_map.hpp>
  14. #include <glad/glad.h>
  15. #include "common/common_types.h"
  16. #include "video_core/engines/const_buffer_info.h"
  17. #include "video_core/engines/maxwell_3d.h"
  18. #include "video_core/rasterizer_cache.h"
  19. #include "video_core/rasterizer_interface.h"
  20. #include "video_core/renderer_opengl/gl_buffer_cache.h"
  21. #include "video_core/renderer_opengl/gl_device.h"
  22. #include "video_core/renderer_opengl/gl_global_cache.h"
  23. #include "video_core/renderer_opengl/gl_rasterizer_cache.h"
  24. #include "video_core/renderer_opengl/gl_resource_manager.h"
  25. #include "video_core/renderer_opengl/gl_sampler_cache.h"
  26. #include "video_core/renderer_opengl/gl_shader_cache.h"
  27. #include "video_core/renderer_opengl/gl_shader_decompiler.h"
  28. #include "video_core/renderer_opengl/gl_shader_manager.h"
  29. #include "video_core/renderer_opengl/gl_state.h"
  30. #include "video_core/renderer_opengl/utils.h"
  31. namespace Core {
  32. class System;
  33. }
  34. namespace Core::Frontend {
  35. class EmuWindow;
  36. }
  37. namespace OpenGL {
  38. struct ScreenInfo;
  39. struct DrawParameters;
  40. struct FramebufferCacheKey;
  41. class RasterizerOpenGL : public VideoCore::RasterizerInterface {
  42. public:
  43. explicit RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window,
  44. ScreenInfo& info);
  45. ~RasterizerOpenGL() override;
  46. void DrawArrays() override;
  47. void Clear() override;
  48. void FlushAll() override;
  49. void FlushRegion(CacheAddr addr, u64 size) override;
  50. void InvalidateRegion(CacheAddr addr, u64 size) override;
  51. void FlushAndInvalidateRegion(CacheAddr addr, u64 size) override;
  52. bool AccelerateSurfaceCopy(const Tegra::Engines::Fermi2D::Regs::Surface& src,
  53. const Tegra::Engines::Fermi2D::Regs::Surface& dst,
  54. const Common::Rectangle<u32>& src_rect,
  55. const Common::Rectangle<u32>& dst_rect) override;
  56. bool AccelerateDisplay(const Tegra::FramebufferConfig& config, VAddr framebuffer_addr,
  57. u32 pixel_stride) override;
  58. bool AccelerateDrawBatch(bool is_indexed) override;
  59. void UpdatePagesCachedCount(VAddr addr, u64 size, int delta) override;
  60. void LoadDiskResources(const std::atomic_bool& stop_loading,
  61. const VideoCore::DiskResourceLoadCallback& callback) override;
  62. /// Maximum supported size that a constbuffer can have in bytes.
  63. static constexpr std::size_t MaxConstbufferSize = 0x10000;
  64. static_assert(MaxConstbufferSize % sizeof(GLvec4) == 0,
  65. "The maximum size of a constbuffer must be a multiple of the size of GLvec4");
  66. private:
  67. struct FramebufferConfigState {
  68. bool using_color_fb{};
  69. bool using_depth_fb{};
  70. bool preserve_contents{};
  71. std::optional<std::size_t> single_color_target;
  72. bool operator==(const FramebufferConfigState& rhs) const {
  73. return std::tie(using_color_fb, using_depth_fb, preserve_contents,
  74. single_color_target) == std::tie(rhs.using_color_fb, rhs.using_depth_fb,
  75. rhs.preserve_contents,
  76. rhs.single_color_target);
  77. }
  78. bool operator!=(const FramebufferConfigState& rhs) const {
  79. return !operator==(rhs);
  80. }
  81. };
  82. /**
  83. * Configures the color and depth framebuffer states.
  84. * @param use_color_fb If true, configure color framebuffers.
  85. * @param using_depth_fb If true, configure the depth/stencil framebuffer.
  86. * @param preserve_contents If true, tries to preserve data from a previously used framebuffer.
  87. * @param single_color_target Specifies if a single color buffer target should be used.
  88. * @returns If depth (first) or stencil (second) are being stored in the bound zeta texture
  89. * (requires using_depth_fb to be true)
  90. */
  91. std::pair<bool, bool> ConfigureFramebuffers(
  92. OpenGLState& current_state, bool use_color_fb = true, bool using_depth_fb = true,
  93. bool preserve_contents = true, std::optional<std::size_t> single_color_target = {});
  94. /// Configures the current constbuffers to use for the draw command.
  95. void SetupDrawConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  96. const Shader& shader);
  97. /// Configures a constant buffer.
  98. void SetupConstBuffer(const Tegra::Engines::ConstBufferInfo& buffer,
  99. const GLShader::ConstBufferEntry& entry);
  100. /// Configures the current global memory entries to use for the draw command.
  101. void SetupGlobalRegions(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
  102. const Shader& shader);
  103. /// Configures the current textures to use for the draw command.
  104. void SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, const Shader& shader,
  105. BaseBindings base_bindings);
  106. /// Syncs the viewport and depth range to match the guest state
  107. void SyncViewport(OpenGLState& current_state);
  108. /// Syncs the clip enabled status to match the guest state
  109. void SyncClipEnabled(
  110. const std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances>& clip_mask);
  111. /// Syncs the clip coefficients to match the guest state
  112. void SyncClipCoef();
  113. /// Syncs the cull mode to match the guest state
  114. void SyncCullMode();
  115. /// Syncs the primitve restart to match the guest state
  116. void SyncPrimitiveRestart();
  117. /// Syncs the depth test state to match the guest state
  118. void SyncDepthTestState();
  119. /// Syncs the stencil test state to match the guest state
  120. void SyncStencilTestState();
  121. /// Syncs the blend state to match the guest state
  122. void SyncBlendState();
  123. /// Syncs the LogicOp state to match the guest state
  124. void SyncLogicOpState();
  125. /// Syncs the the color clamp state
  126. void SyncFragmentColorClampState();
  127. /// Syncs the alpha coverage and alpha to one
  128. void SyncMultiSampleState();
  129. /// Syncs the scissor test state to match the guest state
  130. void SyncScissorTest(OpenGLState& current_state);
  131. /// Syncs the transform feedback state to match the guest state
  132. void SyncTransformFeedback();
  133. /// Syncs the point state to match the guest state
  134. void SyncPointState();
  135. /// Syncs Color Mask
  136. void SyncColorMask();
  137. /// Syncs the polygon offsets
  138. void SyncPolygonOffset();
  139. /// Syncs the alpha test state to match the guest state
  140. void SyncAlphaTest();
  141. /// Check for extension that are not strictly required
  142. /// but are needed for correct emulation
  143. void CheckExtensions();
  144. const Device device;
  145. OpenGLState state;
  146. RasterizerCacheOpenGL res_cache;
  147. ShaderCacheOpenGL shader_cache;
  148. GlobalRegionCacheOpenGL global_cache;
  149. SamplerCacheOpenGL sampler_cache;
  150. Core::System& system;
  151. ScreenInfo& screen_info;
  152. std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
  153. std::map<std::array<Tegra::Engines::Maxwell3D::Regs::VertexAttribute,
  154. Tegra::Engines::Maxwell3D::Regs::NumVertexAttributes>,
  155. OGLVertexArray>
  156. vertex_array_cache;
  157. std::map<FramebufferCacheKey, OGLFramebuffer> framebuffer_cache;
  158. FramebufferConfigState current_framebuffer_config_state;
  159. std::pair<bool, bool> current_depth_stencil_usage{};
  160. static constexpr std::size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;
  161. OGLBufferCache buffer_cache;
  162. BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
  163. BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
  164. std::size_t CalculateVertexArraysSize() const;
  165. std::size_t CalculateIndexBufferSize() const;
  166. /// Updates and returns a vertex array object representing current vertex format
  167. GLuint SetupVertexFormat();
  168. void SetupVertexBuffer(GLuint vao);
  169. DrawParameters SetupDraw();
  170. void SetupShaders(GLenum primitive_mode);
  171. void SetupCachedFramebuffer(const FramebufferCacheKey& fbkey, OpenGLState& current_state);
  172. enum class AccelDraw { Disabled, Arrays, Indexed };
  173. AccelDraw accelerate_draw = AccelDraw::Disabled;
  174. using CachedPageMap = boost::icl::interval_map<u64, int>;
  175. CachedPageMap cached_pages;
  176. };
  177. } // namespace OpenGL